import { getDict } from '@/api/system/dict'

const whiteList = [ 'login', '404' ]

const state = {
  tabsList: [],
  genderOption: [],
  statusOption: []
}

const mutations = {
  SET_TABSLIST: (state, routes) => {
    state.tabsList = routes
  },
  ADD_TAB: (state, route) => {
    state.tabsList.push(route)
  },
  REMOVE_TAB: (state, route) => {
    const { tabsList } = state
    const index = tabsList.findIndex(item => item.fullPath == route.fullPath)
    tabsList.splice(index, 1)
  },
  REMOVE_OTHERS_TAB: (state, route) => {
    let { tabsList } = state
    state.tabsList = tabsList.filter(item => item.fullPath == route.fullPath)
  },
  REMOVE_ALL_TAB: (state, route) => {
    state.tabsList = []
  },
  SET_GENDEROPTION: (state, genderOption) => {
    state.genderOption = genderOption
  },
  SET_STATUSOPTION: (state, statusOption) => {
    state.statusOption = statusOption
  }
}

const actions = {
  async GetGenderOption({ state, commit, rootState }) {
    try {
      const response = await getDict('user_sex')
      const { data } = response
      commit('SET_GENDEROPTION', data)

      return Promise.resolve(response)
    } catch (error) {
      return Promise.reject(error)
    }
  },

  async GetStatusOption({ state, commit, rootState }) {
    try {
      const response = await getDict('status_normal')
      const { data } = response
      commit('SET_STATUSOPTION', data)
      return Promise.resolve(response)
    } catch (error) {
      return Promise.reject(error)
    }
  },

  /**
   * 初始化tabsView
   * @param param0 
   * @param routes 
   */
  InitTabsView({ state, commit }, routes) {
    commit('SET_TABSLIST', routes)
  },

  /** 将路由添加到tabsView中 */
  AddTab({ state, commit }, route) {
    if(whiteList.includes(route.path)) return false
    const isExist = state.tabsList.some((item) => item.fullPath == route.fullPath)
    if(!isExist) commit('ADD_TAB', route)
  },

  RemoveTabsView({ state, commit }, route) {
    commit('REMOVE_TAB', route)
  },

  RemoveOthersTabsView({ state, commit }, route) {
    commit('REMOVE_OTHERS_TAB', route)
  },

  RemoveAllTabsView({ state, commit }) {
    commit('REMOVE_ALL_TAB')
  }

}


export default {
  namespaced: true,
  state,
  mutations,
  actions
}
